package com.estun.backroundManagementService.modules.position.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.estun.backroundManagementService.aop.operationLog.Log;
import com.estun.backroundManagementService.aop.operationLog.LogConstants;
import com.estun.backroundManagementService.domain.BasePageRequest;
import com.estun.backroundManagementService.domain.BaseResponse;
import com.estun.backroundManagementService.modules.position.model.Device;
import com.estun.backroundManagementService.modules.position.service.DeviceService;
import com.estun.backroundManagementService.utils.QueryWrapperUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 工艺
 */
@RestController
@RequestMapping("/device")
@RequiredArgsConstructor
public class DeviceController {

    private final DeviceService deviceService;

    @PostMapping("/page")
    public BaseResponse page(@RequestBody BasePageRequest<Device> pageRequest) {
        Page<Device> page = new Page<>(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<Device> queryWrapper = QueryWrapperUtil.buildQueryWrapper(pageRequest.getQuery());
        Page<Device> page1 = deviceService.page(page, queryWrapper);
        return BaseResponse.success(page1);
    }

    @GetMapping("/{id}")
    public BaseResponse getDeviceById(@PathVariable String id) {
        return BaseResponse.success(deviceService.getById(id));
    }

    @PostMapping("/add")
    @Log(module = "设备管理 新增", type = LogConstants.INSERT, desc = "设备管理 新增")
    public BaseResponse addDevice(@RequestBody Device device) {
        return BaseResponse.success(deviceService.save(device));
    }

    @PutMapping("/update")
    @Log(module = "设备管理 更新", type = LogConstants.UPDATE, desc = "设备管理 更新")
    public BaseResponse updateDevice(@RequestBody Device device) {
        return BaseResponse.success(deviceService.updateById(device));
    }

    @DeleteMapping("/delete/{id}")
    @Log(module = "设备管理 删除", type = LogConstants.DELETE, desc = "设备管理 删除")
    public BaseResponse deleteDevice(@PathVariable String id) {
        return BaseResponse.success(deviceService.removeById(id));
    }


    @ApiOperation("设备管理 查询所有")
    @GetMapping(value = "/list")
    @ResponseBody
    public BaseResponse list() {
        return BaseResponse.success(deviceService.list());
    }

}
